<?php


namespace app\admin\traits;


use think\Exception;

trait GetTableData
{

    //获取每个列表页的数据(index table 数据)
    protected static function operateDatum(array $where, array $fields, array $order, array $ruleWhere = null){
        if (!isset($fields['fields']) || !isset($fields['with'])) throw new Exception('参数错误.');
        //整理条件
        $page  = request()->get('page/d', 1);
        $limit = request()->get('limit/d', 20);

        //如果传递了权限规则条件 就合并到条件里
        if ($ruleWhere) $where[] = $ruleWhere;

        //返回的数据
        $count = static::where($where)
            ->count();

        $list = static::where($where)
            ->order($order)
            ->page($page)
            ->limit($limit)
            ->with($fields['with'])
            ->withCount($fields['withCount'])
            ->field($fields['fields'])
            ->select()
            ->toArray();
        return [
            'count' => $count,
            'list' => $list
        ];
    }

}